import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Change from '../views/ChangePassword'
import Message from '../views/Message'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/change',
    name: 'change',
    component: Change
  },
  {
    path: '/message',
    name: 'Message',
    component: Message
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=> {
  //to将要访问
  //from从哪访问
  //next接着干  重定向到url上next（）继续访问到tor路径
  if (to.path == '/login') return next();
  //获取user
  const userFlag = window.sessionStorage.getItem("user");//取出用户
  if (!userFlag) return next('/login');
  next();
})
export default router
